package com.feiluan;

import com.feiluan.listener.NettyServerListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.TaskExecutor;

import javax.annotation.Resource;


/**
 * Spring Boot Initializer Class
 *
 * @author Moniruzzaman Md
 *
 */
@SpringBootApplication
@EnableCaching
@ConfigurationProperties(prefix = "spring.cache.redis")
public class Application extends SpringBootServletInitializer implements CommandLineRunner {

	@Autowired
	private Runnable MqttMessageListener;

	@Resource
	private NettyServerListener nettyServerListener;

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(Application.class);
	}

	public static void main(String[] args) {

//		ApplicationContext context = SpringApplication.run(Application.class, args);
//		NettyServerListener nettyServer = context.getBean(NettyServerListener.class);
//		nettyServer.start();

		SpringApplication.run(Application.class, args);


	}

	@Bean
	public CommandLineRunner schedulingRunner(TaskExecutor executor) {
		return new CommandLineRunner() {
			@Override
			public void run(String... args) throws Exception {
				executor.execute(MqttMessageListener);
			}
		};
	}

	@Override
	public void run(String... args) {
		nettyServerListener.start();
	}


}
